package pl.prv.polanie.client.widgets;

import pl.prv.polanie.client.Planner;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class CustomVerticalPanel extends VerticalPanel {
	
	private static final String STYLE__CVP_NO_ITEMS_MSG_HTML = "noItemsMsgHTML";
	
	private String noItemsMessageText = "no items";
	
	private int addedElementsCount = 0;
	
	private Widget noItemsMsgWidget = null;
	
	public CustomVerticalPanel(String noItemsMessageText) {
		this.noItemsMessageText = noItemsMessageText;
	}
	
	
	@Override
	public void clear() {
		super.clear();
		
		Planner.log("CustomVerticalPanel.add: " + "Clearuje liste z linkami do podzialow");
		addedElementsCount = 0;
		showNoItemsMessage();
	}

	@Override
	public void add(Widget w) {
		if (addedElementsCount == 0) {
//			Planner.log("CustomVerticalPanel.add: " + "CLEAR");
			hideNoItemsMessage();
		}
		
		super.add(w);
		addedElementsCount = addedElementsCount + 1;
	}

	@Override
	public void insert(Widget w, int beforeIndex) {
		if (addedElementsCount == 0) {
			hideNoItemsMessage();
		}
		
		super.insert(w, beforeIndex);
		addedElementsCount++;
	}

	@Override
	public boolean remove(Widget w) {
		boolean result = super.remove(w);
		if (result) {
			addedElementsCount--;
			if (addedElementsCount == 0) {
				//TODO pokazac element
				showNoItemsMessage();
			}
		}
			
		return result; 
	}

	private void showNoItemsMessage() {
		HTML msgHTML = new HTML(noItemsMessageText);
		noItemsMsgWidget = msgHTML;
		msgHTML.addStyleName(STYLE__CVP_NO_ITEMS_MSG_HTML);
		super.add(msgHTML);
	}
	
	private void hideNoItemsMessage() {
		if (noItemsMsgWidget != null) {
			super.remove(noItemsMsgWidget);
		}
	}
	
}
